#include <iostream>
#include <algorithm>
using namespace std;
struct DATA
{
	int val;
	int head;
	int length;
};

bool cmp(DATA a,DATA b)
{
	if(a.length!=b.length){
		return a.head>b.head;
	}
	else{
		return a.val>b.val;
	}
}
int main()
{
	DATA src[1000];
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int temp;
		cin>>temp;
		src[i].val=temp;
		int len=0;
		while(temp>=10){
			temp=temp/10;
			len++;
		}
		src[i].head=temp;
		src[i].length=len+1;
	}
	sort(src,src+n,cmp);
	for(int i=0;i<n;i++){
		cout<<src[i].val<<endl;
	}
	for(int j=0;j<n;j++){
		cout<<src[j].val;
	}
	cout<<endl;
	return 0;
} 
